/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.facades;

import db.facadelocals.GPFacadeLocal;
import db.*;
import db.entities.GP;
import details.GPDetails;
import exceptions.GPNotFoundException;
import exceptions.UsernameAlreadyExistsException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author jerre
 */
@Stateless
public class GPFacade implements GPFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void registerGP(String username, String password) throws UsernameAlreadyExistsException {
        if(findByUsername(username) == null){
            GP gp = new GP(username, password);
            create(gp);
        } else {
            throw new UsernameAlreadyExistsException(username);
        }
    }

    public void registerGP(String username, String password, String name, String surname) throws UsernameAlreadyExistsException {
        if(findByUsername(username) == null){
            GP gp = new GP(username, password, name, surname);
            create(gp);
        } else {
            throw new UsernameAlreadyExistsException(username);
        }
    }

    public void setPassword(String username, String password) throws GPNotFoundException {
        GP gp = findByUsername(username);
        if(gp != null){
            gp.setPassword(password);
            edit(gp);
        } else {
            throw new GPNotFoundException(username);
        }
    }

    public void setName(String username, String name) throws GPNotFoundException {
        GP gp = findByUsername(username);
        if(gp != null){
            gp.setName(name);
            edit(gp);
        } else {
            throw new GPNotFoundException(username);
        }
    }

    public void setSurName(String username, String surName) throws GPNotFoundException {
        GP gp = findByUsername(username);
        if(gp != null){
            gp.setSurname(surName);
            edit(gp);
        } else {
            throw new GPNotFoundException(username);
        }
    }

    public List<String> getPatientsFromGP(String usernameGP) throws GPNotFoundException {
        GP gp = findByUsername(usernameGP);
        if(gp != null){
            return gp.getPatients();
        } else {
            throw new GPNotFoundException(usernameGP);
        }
    }

    public void addPatientToGP(String usernameGP, String usernamePatient) throws GPNotFoundException {
        GP gp = findByUsername(usernameGP);
        if(gp != null){
            gp.addPatient(usernamePatient);
            edit(gp);
        } else {
            throw new GPNotFoundException(usernameGP);
        }
    }

    public void removePatientFromGP(String usernameGP, String usernamePatient) throws GPNotFoundException {
        GP gp = findByUsername(usernameGP);
        if(gp != null){
            gp.removePatient(usernamePatient);
            edit(gp);
        } else {
            throw new GPNotFoundException(usernameGP);
        }
    }

    public GPDetails getGP(String username) throws GPNotFoundException {
        GP gp = findByUsername(username);
        if(gp != null){
            GPDetails gpDetails = new GPDetails(gp.getId(), gp.getUsername(), gp.getPassword(), gp.getName(), gp.getSurname());

            return gpDetails;
        } else {
            throw new GPNotFoundException(username);
        }
    }

    public List<GPDetails> getAllGPs() {
        List<GP> list = findAll();
        List<GPDetails> gps = new ArrayList<GPDetails>();

        for(GP gp : list){
            gps.add(new GPDetails(gp.getId(), gp.getUsername(), gp.getPassword(), gp.getName(), gp.getSurname()));
        }

        return gps;
    }

    public boolean existGP(String username){
        GP gp = findByUsername(username);
        if(gp != null)
            return true;
        else
            return false;
    }
    
    public GP findByUsername(String username) {
        try {
            GP gp = (GP) em.createNamedQuery("findGPByUsername").setParameter("username", username).getSingleResult();
            return gp;
        } catch (Exception e) {
            return null;
        }
    }

    public void create(GP gP) {
        em.persist(gP);
    }

    public void edit(GP gP) {
        em.merge(gP);
    }

    public void remove(GP gP) {
        em.remove(em.merge(gP));
    }

    public GP find(Object id) {
        return em.find(GP.class, id);
    }

    public List<GP> findAll() {
        return em.createQuery("select object(o) from GP as o").getResultList();
    }


}
